package com.back.campus_back.service.impl;


import com.back.campus_back.domain.Collect;
import com.back.campus_back.domain.Goods;
import com.back.campus_back.domain.ShoppingCart;
import com.back.campus_back.mapper.ShoppingCartMapper;
import com.back.campus_back.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-17
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    //根据用户查询对应的购物车信息
    public List getShoppingCart(@Param(value = "user_id") int user_id){
        return shoppingCartMapper.getShoppingCart(user_id);
    }

    //  查询购物车的某一条商品信息
    public List<Goods> getShoppingCartOne( int user_id, int goods_id){
        return shoppingCartMapper.getShoppingCartOne(user_id,goods_id);
    }


    //个人添加商品到购物车
    public Map<String,List> addShoppingCart( int user_id,  int goods_id){

        //添加之前，检查一下购物车是否存在，存在则返回
        List<Goods> sCart=getShoppingCartOne(user_id,goods_id);
        if(sCart.size()!=0){
            List<String> li = new ArrayList<String>();
            li.add("商品已存在");
            Map<String,List> m=new HashMap<String,List>();
            m.put("msg",li);
            return m;
        }

        //添加商品
        int result = shoppingCartMapper.addShoppingCart(user_id,goods_id);
        if(result == 1){
            List<Goods> sCart2=getShoppingCartOne(user_id,goods_id);
            List<String> l = new ArrayList<String>();
            l.add("添加购物车成功");

            Map<String,List> m=new HashMap<String,List>();
            m.put("datalist",sCart2);
            m.put("msg",l);

            return m;
        }else{
            List<String> l = new ArrayList<String>();
            l.add("添加失败");
            Map<String,List> m=new HashMap<String,List>();
            m.put("msg",l);
            return m;
        }
    }


    //删除购物车的商品
    public String deleteShoppingCart( int user_id,  int goods_id){
        int result = shoppingCartMapper.deleteShoppingCart(user_id,goods_id);
        if(result == 1){
            return "删除成功";
        }else{
            return "删除失败";
        }

    }




}
